using System;
using Rapier.Kodachi.Shared;
using System.Net;
using System.Data;
using System.Drawing;

namespace Rapier.Kodachi.WebSearch
{
    /*
     * This is a specific action named "Search For" that is used with search engine Items
     */
    public class SearchFor : Action
    {
        #region Constructor
        public SearchFor(Plugin plugin)
            : base(plugin, "Rapier.Kodachi.Weather.SearchFor", "Search for...", "Use the currently selected search engine to search the web.")
        {
            this.IndirectItemUse = IndirectItemUse.Required;
            this.DefaultIndirectTextMode = true;
            GetImage = delegate() { return Resource.Search; };
        }
        #endregion

        #region Action Members
        public override void Execute(Item[] directItems, Item[] indirectItems)
        {
            foreach(Item directItem in directItems)
            {
                WebSearchEngine engine = (WebSearchEngine)directItem.Properties["WebSearchEngine"];
                foreach (Item indirectItem in indirectItems)
                {
                    string url = String.Format(engine.QueryFormat, indirectItem.Properties["Text"].ToString());
                    System.Diagnostics.Process.Start(url);
                }
            }
        }

        public override bool IsValidDirect(Item directItem)
        {
            return directItem.Properties.ContainsKey("WebSearchEngine");
        }

        public override bool IsValidIndirect(Item indirectItem)
        {
            return indirectItem.Properties.ContainsKey("Text");
        }
        #endregion
    }
}
